package org.web.core;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import java.beans.PropertyEditorSupport;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;


public class SimpleController implements Serializable {
    private static final long serialVersionUID = 7240970012270690926L;
    protected Logger log = LoggerFactory.getLogger(getClass());
    protected LinkedHashMap<String, Object> resultMap = new LinkedHashMap<String,Object>();

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        //SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
        public void setAsText(String value) {
            try {
                setValue(new SimpleDateFormat("yyyy-MM-dd").parse(value));
            } catch(ParseException e) {
                setValue(null);
            }
        }

        public String getAsText() {
            return new SimpleDateFormat("yyyy-MM-dd").format((Date) getValue());
        }

    });
    }

    public LinkedHashMap<String, Object> getResultMap() {
        return resultMap;
    }

    public void setResultMap(LinkedHashMap<String, Object> resultMap) {
        this.resultMap = resultMap;
    }

}
